feat(cli): auto-dispose InstanceContext after effectCmd handlers#25481
Merged
kitlangton merged 1 commit intodevfrom May 2, 2026
Merged
feat(cli): auto-dispose InstanceContext after effectCmd handlers#25481kitlangton merged 1 commit intodevfrom
kitlangton merged 1 commit intodevfrom
Conversation
kitlangton
commented
May 2, 2026
| const directory = opts.directory?.(args) ?? process.cwd() | ||
| await AppRuntime.runPromise(InstanceStore.Service.use((s) => s.provide({ directory }, opts.handler(args)))) | ||
| await AppRuntime.runPromise( | ||
| InstanceStore.Service.use((store) => |
kitlangton
commented
May 2, 2026
| @@ -2,6 +2,7 @@ import type { Argv } from "yargs" | |||
| import { Effect, Schema } from "effect" | |||
kitlangton
commented
May 2, 2026
| const directory = opts.directory?.(args) ?? process.cwd() | ||
| await AppRuntime.runPromise(InstanceStore.Service.use((s) => s.provide({ directory }, opts.handler(args)))) | ||
| await AppRuntime.runPromise( | ||
| InstanceStore.Service.use((store) => |
kitlangton
commented
May 2, 2026
| const directory = opts.directory?.(args) ?? process.cwd() | ||
| await AppRuntime.runPromise(InstanceStore.Service.use((s) => s.provide({ directory }, opts.handler(args)))) | ||
| await AppRuntime.runPromise( | ||
| InstanceStore.Service.use((store) => |
Wrap the user handler in Effect.ensuring(store.dispose(ctx)) inside the effectCmd factory so per-command disposal is automatic — runs disposers + emits server.instance.disposed on every Exit (success / typed failure / defect / interruption). Matches the legacy bootstrap() finally without 14× hand-rolled boilerplate at the call sites. Idempotent: existing commands that still call store.dispose(ctx) explicitly inside their handler bodies will see the second auto-dispose call become a no-op (cache entry already removed; disposeContext doesn't re-fire).
dc7cff5 to
00bee6c
Compare
aprogramq
pushed a commit
to aprogramq/opencode
that referenced
this pull request
May 3, 2026
5 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Wrap the user handler in `Effect.ensuring(store.dispose(ctx))` inside the `effectCmd` factory so per-command disposal is automatic — runs disposers and emits `server.instance.disposed` on every Exit (success / typed failure / defect / interruption). Matches the legacy `bootstrap()` finally semantics without per-handler boilerplate.
Two parallel subagent reviews of the debug-conversion PR (#25479) flagged the 14× repeated `const ctx = yield* InstanceRef; if (!ctx) return; const store = yield* InstanceStore.Service; return yield* body.pipe(Effect.ensuring(store.dispose(ctx)))` pattern as a footgun. This PR moves the responsibility into `effectCmd` itself.
Behavioral notes
Followup (not in this PR)
Drop the now-redundant explicit `Effect.ensuring(store.dispose(ctx))` from `import.ts`, `export.ts`, `stats.ts`, and `debug/*.ts` once those PRs land. Keeps things tidy without breaking semantics in the meantime.
Test plan